const app = getApp();
var publicApi = require('../../script/public.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navHeight: app.globalData.statusBarHeight,
    isSetMode: false,
    RouteMenus: [{
        MENU_ID: 390101,
        TITLE: '待执行',
        ICON: 'order_3.png',
        PATH: '/packageOrder/todoOrder/todoOrder',
      },
      {
        MENU_ID: 390102,
        TITLE: '待审核',
        ICON: 'order_1.png',
        PATH: '/packageOrder/examineOrder/examineOrder',
      },
      {
        MENU_ID: 390103,
        TITLE: '待分派',
        ICON: 'order_16.png',
        PATH: '/packageOrder/dispitchOrder/dispitchOrder',
      },
      {
        MENU_ID: 390104,
        TITLE: '待受理',
        ICON: 'order_17.png',
        PATH: '/packageOrder/accept/accept',
      },
      {
        MENU_ID: 390105,
        TITLE: '工单池',
        ICON: 'order_14.png',
        PATH: '/packageOrder/orderPow/orderPow',
      },
      {
        MENU_ID: 390106,
        TITLE: '二次派单',
        ICON: 'order_2.png',
        PATH: '/packageOrder/reOrder/reOrder',
      },
      {
        MENU_ID: 390107,
        TITLE: '催办',
        ICON: 'order_11.png',
        PATH: '/packageOrder/reminder/reminder',
      },
      {
        MENU_ID: 390201,
        TITLE: '供热收费',
        PATH: '/packageRevenue/charge-search/charge-search',
        ICON: 'grsf.png',
      },
      {
        MENU_ID: 390202,
        TITLE: '移动抄表',
        PATH: '/packageRevenue/meter-control/meter-control',
        ICON: 'ydcb.png',
      },
      {
        MENU_ID: 390203,
        TITLE: '我的收费',
        PATH: '/packageRevenue/pay-list/pay-list',
        ICON: 'jfjl.png',
      },
      {
        MENU_ID: 390301,
        TITLE: '稽查计划',
        PATH: '/packageAudit/audit-plan/audit-plan',
        ICON: 'jcjh.png',
      },
      {
        MENU_ID: 390302,
        TITLE: '稽查工单',
        PATH: '/packageAudit/audit-order/audit-order',
        ICON: 'jcgd.png',
      },
      {
        MENU_ID: 390303,
        TITLE: '客户查询',
        PATH: '/packageAudit/user-search/user-search',
        ICON: 'khcx.png',
      },
      {
        MENU_ID: 390304,
        TITLE: '我的工单',
        PATH: '/packageAudit/my-order/my-order',
        ICON: 'wdgd.png',
      },
      {
        MENU_ID: 390401,
        TITLE: '待办任务',
        PATH: '/packageSys/flow-do/flow-do',
        ICON: 'dbrw.png',
      },
      {
        MENU_ID: 390402,
        TITLE: '已办任务',
        PATH: '/packageSys/flow-done/flow-done',
        ICON: 'ybrw.png',
      },
    ],
    setMenuList: [],
    menuGroupList: [],
    menuGroupListCopy: [],
    ReachBottom: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getSetMenuList();
    this.GetAppMenu();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.reSet();
    this.getSetMenuList();
    this.GetAppMenu();
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      ReachBottom: true
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  reSet() {
    this.setData({
      isSetMode: false,
      setMenuList: [],
      menuGroupList: []
    });
  },
  getSetMenuList() {
    const setMenuList = wx.getStorageSync('setMenu');
    if (setMenuList && setMenuList != '' && setMenuList.length > 0) {
      this.setData({
        setMenuList
      })
    }
  },
  GetAppMenu() {
    publicApi.GetAppMenu().then(res => {
      if (res.code === 200) {
        const resData = res.data && res.data.length > 0 ? res.data[0].Children : [];
        resData.sort((a, b) => {
          return a.SORT_NO - b.SORT_NO
        });
        this.getMenuGroup(resData);
      }
    })
  },
  getMenuGroup(list) {
    if (list.length === 0) return;
    list.forEach(group => {
      group.Children.forEach(menu => {
        const routeMenu = this.data.RouteMenus.find(m => m.MENU_ID === menu.MENU_ID);
        const index = this.data.setMenuList.findIndex(n => n.MENU_ID === menu.MENU_ID);
        if (routeMenu) {
          menu.ICON = routeMenu ? routeMenu.ICON : 'order_2.png';
          menu.PATH = routeMenu ? routeMenu.PATH : 'empty';
          menu.isSet = index > -1;
        }
      })
    })
    this.setData({
      menuGroupList: list
    });
  },
  toPage(route) {
    wx.navigateTo({
      url: route
    });
  },
  setFavMenu(menu) {
    wx.getStorage({
      key: 'favmenu',
      success(res) {
        const favmenu = res.data;
        const thisMenu = favmenu.find(m => m.MENU_ID === menu.MENU_ID);
        if (thisMenu) {
          thisMenu.VisitCount += 1;
        } else {
          favmenu.push({
            MENU_ID: menu.MENU_ID,
            PATH: menu.PATH,
            TITLE: menu.TITLE,
            ICON: menu.ICON,
            VisitCount: 1
          })
        }
        wx.setStorageSync('favmenu', favmenu);
      },
      fail(err) {
        const newMenu = {
          MENU_ID: menu.MENU_ID,
          TITLE: menu.TITLE,
          PATH: menu.PATH,
          ICON: menu.ICON,
          VisitCount: 1
        }
        wx.setStorageSync('favmenu', [newMenu]);
      }
    })
  },
  setEdit() {
    this.setData({
      isSetMode: true,
      menuGroupListCopy: JSON.parse(JSON.stringify(this.data.menuGroupList))
    })
  },
  hasSet(menu) {
    const index = this.data.setMenuList.findIndex(item => {
      return item.MENU_ID === menu.MENU_ID
    })
    return index > -1;
  },
  menuClick(e) {
    const type = e.currentTarget.dataset.type;
    const menu = e.currentTarget.dataset.value;
    if (this.data.isSetMode) {
      const setMenuList = [...this.data.setMenuList];
      let flag = false;
      if (type === 'add') {
        if (menu.isSet) return;
        setMenuList.push({
          MENU_ID: menu.MENU_ID,
          TITLE: menu.TITLE,
          PATH: menu.PATH,
          ICON: menu.ICON,
        });
        flag = true;
      } else {
        const index = setMenuList.findIndex(item => item.MENU_ID === menu.MENU_ID);
        if (index > -1) {
          setMenuList.splice(index, 1);
        }
        flag = false;
      }
      this.updateSetStatus(menu.MENU_ID, flag);
      this.setData({
        setMenuList,
      })
    } else {
      this.setFavMenu(menu);
      this.toPage(menu.PATH);
    }
  },
  updateSetStatus(MENU_ID, flag) {
    const menuGroupList = [...this.data.menuGroupList];
    for (let i = 0; i < menuGroupList.length; i++) {
      const Menus = menuGroupList[i].Children;
      for (let j = 0; j < Menus.length; j++) {
        if (Menus[j].MENU_ID === MENU_ID) {
          Menus[j].isSet = flag;
          break
        }
      }
    }
    this.setData({
      menuGroupList
    })
  },
  save() {
    this.setData({
      isSetMode: false,
      menuGroupListCopy: JSON.parse(JSON.stringify(this.data.menuGroupList))
    });
    wx.setStorageSync('setMenu', this.data.setMenuList);
    wx.showToast({
      title: '保存成功！',
      icon: 'none',
      duration: 2000
    })
  },
  doCancel() {
    wx.showModal({
      content: '编辑的内容还没保存，是否要保存？',
      success: (res) => {
        if (res.confirm) {
          this.save();
        } else if (res.cancel) {
          this.setData({
            isSetMode: false,
            menuGroupList: JSON.parse(JSON.stringify(this.data.menuGroupListCopy))
          });
          this.getSetMenuList();
        }
      }
    })
  },
  back() {
    wx.navigateBack();
  },
})